/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.aileron.jww.admin.model.logic;

import java.util.HashMap;

import cc.aileron.jww.admin.model.condition.PagingCondition;
import cc.aileron.jww.admin.model.dao.EditHeaderDao;
import cc.aileron.jww.admin.model.dao.EditItemDao;
import cc.aileron.jww.admin.model.dto.EditItemColumn;

import com.google.inject.Inject;

/**
 * EditItemLogic　実装
 * @author Aileron
 *
 */
public class EditItemLogicImpl implements EditItemLogic
{
    /* (非 Javadoc)
     * @see cc.aileron.jww.admin.model.logic.EditItemLogic#get(int)
     */
    @Override
    public Iterable<HashMap<String, Object>> get(
            final int id,
            final int page)
    {
        final PagingCondition condition = new PagingCondition( menuItemLogic.getItem(id).name );
        condition.pageNumber = page;
        return dao.findByCondition(condition);
    }
    
    /*
     * (非 Javadoc)
     * @see cc.aileron.jww.admin.model.logic.EditItemLogic#getHeader()
     */
    @Override
    public HashMap<Integer, HashMap<String, String>> getHeader()
    {
        return map;
    }

    /**
     * constractor injection
     * @param editItemDao
     * @param menuItemDao
     */
    @Inject
    public EditItemLogicImpl(
            final EditItemDao editItemDao,
            final EditHeaderDao editHeaderDao,
            final MenuItemLogic menuItemLogic)
    {
        this.menuItemLogic = menuItemLogic;
        this.dao = editItemDao;
        
        for(final EditItemColumn c : editHeaderDao.findByAll())
        {
            if(!map.containsKey(c.id))
            {
                map.put(c.id, new HashMap<String, String>());
            }
            map.get(c.id).put(c.name, c.label);
        }
    }
    
    /**
     * ヘッダー情報一覧
     */
    private final HashMap<Integer,HashMap<String,String>> map = new HashMap<Integer, HashMap<String,String>>();
    
    /**
     * dao
     */
    private final EditItemDao dao ;
    
    /**
     * menuItemLogic
     */
    private final MenuItemLogic menuItemLogic;
}
